package com.gym.controller;

import com.github.pagehelper.PageInfo;
import com.gym.pojo.*;
import com.gym.service.CashierService;
import com.gym.service.GoodsService;
import com.gym.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
public class GoodsController {
    @Autowired
    private GoodsService service;
    @Autowired
    private PurchaseService purservice;
    @Autowired
    private CashierService cashierservice;
    @RequestMapping("/selectcp")
    public Map<String,Object> selectcp(String goodsname,int page,int limit){
        PageInfo<Goods> pge=service.selectcp(goodsname,page,limit);
        System.err.println(pge);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", 0);
        result.put("msg", "");
        result.put("count",pge.getTotal());
        result.put("data", pge.getList());
        return result;
    }
    @RequestMapping("/insertsm")
    public boolean insertsm(@RequestBody Purchase purchase,int totalstock){
        service.updatejhj(totalstock+purchase.getQuantity(), purchase.getGoodsId());
        System.out.println("daww");
        return purservice.insertsm(purchase);
    }
    @RequestMapping("selectzj")
    public List<Goods> selectzj(){
        return service.selectzj();
    }
    @RequestMapping("insertzj")
    public boolean insertzj(@RequestBody Cashier cashier) {
        service.updatezj(cashier.getGoodsId(),cashier.getQuantity());
        return cashierservice.insertzj(cashier);
    }
    @RequestMapping("selectysp")
    public boolean selectysp(String goodsname){
        return service.selectysp(goodsname);
    }
    @RequestMapping ("/deletezj")
    public boolean deletezj(int cashierId){
        return cashierservice.deletezj(cashierId);
    }
    @RequestMapping("/selectsm")
    public Map<String, Object> selectsm(Integer cashierId,int page,int limit){
        PageInfo<Cashier> pge=cashierservice.selectsm(cashierId,page,limit);
        System.err.println(pge);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", 0);
        result.put("msg", "");
        result.put("count",pge.getTotal());
        result.put("data", pge.getList());
        return result;
    }
    @RequestMapping("selectzkc")
    public boolean selectzkc(int goodsId,int totalstock){
        return service.selectzkc(goodsId,totalstock);
    }
    @RequestMapping("selectsl")
    public boolean selectsl(int goodsId,int goodsamount){
        return service.selectsl(goodsId,goodsamount);
    }
    @RequestMapping("/insertjh")
    public boolean insertjh(@RequestBody Goods goods) {
        return service.insertjh(goods);
    }
    @RequestMapping("updatejj")
    public boolean updatejj(int totalstock, int goodsId, int goodsamount){
        service.updatejhj(totalstock-goodsamount,goodsId);
        return service.updatejj(totalstock,goodsId,goodsamount);
    }
    @RequestMapping("deletesc")
    public boolean deletesc(int goodsId){
        purservice.deletethz(goodsId);
        cashierservice.deleteth(goodsId);
        return service.deletesc(goodsId);
    }
}
